#!/usr/bin/env bash

# what-node.sh

# This program is part of Faridayix, an artificially intelligent computer
# operating system.

# Copyright (C) 2010 Scott C. MacCallum
# scottmaccal@gmail.com

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

#       |                                  80 Columns                                    |

# Programs, control characters, and options used by what-node.sh:

# printf (print function)
# \n (new line control character)

# sudo (super user do)

# fping (file ping)
# -g (Generate a target list from a supplied Internet protocol netmask, or a
# starting and ending Internet protocol address.)

# sed (stream editor)

# sort

# nmap (network map)

# Find out what nodes will respond to a ping in the user specified network
# range and gather information about each one.

printf "\n"
printf "Sending ping to nodes in user specified Internet protocol network range."
printf "\n\n"

sleep 3

sudo fping -g 172.20.0.1 172.20.8.254 > fping-out000.txt

# Delete all lines with "error while sending ping: Permission denied" from the
# stream of the previous output file and create a new output file with this
# change.

sed '/error while sending ping: Permission denied/d' fping-out000.txt > fping-out001.txt

# Delete all lines with "error while sending ping: No buffer space available"
# from the stream of the previous output file and create a new output file
# with this change.

sed '/error while sending ping: No buffer space available/d' fping-out001.txt > fping-out002.txt

# Delete all lines with "is unreachable" from the previous output file stream
# and create a new output file with this change.

sed '/is unreachable/d' fping-out002.txt > fping-out003.txt

# Substitute "is alive" from all lines in the previous output file stream with
# " " and create a new output file with this change.

sed 's/ is alive/ /' fping-out003.txt > fping-out004.txt

# Delete all white space from the previous output file stream and create a new
# output file with this change.

sed 's/ *$//' fping-out004.txt > fping-out005.txt

# Delete all blank lines from the previous output file stream and create a new
# output file with this change.

sed '/^$/d' fping-out005.txt > fping-out006.txt

# Sort all Internet protocol addresses numerically lowest to highest from the
# previous output file and create a new output file with this change.

sort -t . -k 3,3n -k 4,4n fping-out006.txt > fping-out007.txt

# Scan each node and produce its host name, machine access control address,
# open port, and operating system using the previous output file and create a
# new output file containing these findings.

printf "\n"
printf "Scanning nodes that responded to a ping."
printf "\n\n"

sudo nmap -O -iL fping-out007.txt > nmap-out000.txt

# Delete all lines with "Starting Nmap" from the previous output file stream
# and create a new output file with this change.

sed '/Starting Nmap/d' nmap-out000.txt > nmap-out001.txt

# Substitute "Interesting ports on" from all lines in the previous output file
# stream with " " and create a new output file with this change.

sed 's/Interesting ports on/ /' nmap-out001.txt > nmap-out002.txt

# Substitute "(" from all lines in the previous output file stream with " "
# and create a new output file with this change.

sed 's/(/ /' nmap-out002.txt > nmap-out003.txt

# Substitute ")" from all lines in the previous output file stream with " "
# and create a new output file with this change.

sed 's/)/ /' nmap-out003.txt > nmap-out004.txt

# Substitute the first occurrence of ":" in the previous output file stream
# with " " and create a new output file with this change.

sed 's/:/ /' nmap-out004.txt > nmap-out005.txt

# Substitute all occurrence of "|' in the previous output file stream
# with " " and create a new output file with this change.

sed 's/|/ /g' nmap-out005.txt > nmap-out006.txt

# Delete all lines with "Not shown" from the stream of the previous output
# file create a new output file with this change.

sed '/Not shown/d' nmap-out006.txt > nmap-out007.txt

# Delete all lines with "PORT" from the stream of the previous output file and
# create a new output file with this change.

sed '/PORT/d' nmap-out007.txt > nmap-out008.txt

# Substitute "/" from all lines in the stream of the previous output file with
# " " and create a new output file with this change.

sed 's:/: :g' nmap-out008.txt > nmap-out009.txt

# Substitute "MAC Address" from all lines in the stream of the previous output
# file with " " and create a new output file with this change.

sed 's/MAC Address/ /' nmap-out009.txt > nmap-out010.txt

# Delete all lines with "Device type" from the stream of the previous output
# file and create a new output file with this change.

sed '/Device type/d' nmap-out010.txt > nmap-out011.txt

# Substitute "Running" from all lines in the stream of the previous output
# file with " " and create a new output file with this change.

sed 's/Running/ /' nmap-out011.txt > nmap-out012.txt

# Delete all lines with "Device type" from the stream of the previous output
# file and create a new output file with this change.

sed '/Device type/d' nmap-out012.txt > nmap-out013.txt

# Delete all lines with "OS details" from the stream of the previous output
# file and create a new output file with this change.

sed '/OS details/d' nmap-out013.txt > nmap-out014.txt

# Delete all lines with "Network Distance" from the stream of the previous
# output file and create a new output file with this change.

sed '/Network Distance/d' nmap-out014.txt > nmap-out015.txt

# Delete all lines with "All 1000 scanned" from the stream of the previous
# output file and create a new output file with this change.

sed '/All 1000 scanned/d' nmap-out015.txt > nmap-out016.txt

# Delete all lines with "Warning  OSScan results" and from the stream of the
# previous output file and create a new output file with this change.

sed '/Warning  OSScan results/d' nmap-out016.txt > nmap-out017.txt

sed '/No/d' nmap-out017.txt > nmap-out018.txt

sed '/TCP/d' nmap-out018.txt > nmap-out019.txt

sed '/OS/d' nmap-out019.txt > nmap-out020.txt

sed '/JUST/d' nmap-out020.txt > nmap-out021.txt

# Delete all blank lines from the previous output file stream and create a
# new output file with this change.

sed '/^$/d' nmap-out021.txt > nmap-out022.txt

sed -e 's/^[ \t]*//' nmap-out022.txt > nmap-out023.txt